package net.uman.web.manager;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.uman.entity.Resource;
import net.uman.framework.modules.web.struts2.Struts2Utils;
import net.uman.service.ResourceManager;
import net.uman.web.CrudActionSupport;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
@Namespace("/manager")
public class ResourceAction extends CrudActionSupport<Resource> {
	private static final long serialVersionUID = -6859411297210213983L;
	@Autowired
	private ResourceManager resourceManager;
	/**
	 * 添加资源页面
	 * @return
	 */
	public String addResource() {
		return "addResource";
	}
	/**
	 * 获取所有资源
	 * @return
	 */
	public String getAllResource() {
		renderResourceListJsonData();
		return null;
	}
	/**
	 * 发送资源list的json数据
	 * @param typeString
	 */
	private void renderResourceListJsonData() {
		JSONObject jsonobject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		
		List<Resource> resources = resourceManager.getResourceList();
		
		for(Resource resource : resources){
			JSONObject json = new JSONObject();
			json.put("addUserName", resource.getAddUserName());
			json.put("addDate", resource.getAddDate().toLocaleString());
			json.put("description", resource.getDescription());
			json.put("needPoint", resource.getNeedPoint());
			json.put("url", resource.getUrl());
			json.put("name", resource.getName());
			jsonArray.add(json);
		}
		
		jsonobject.put("rows", jsonArray);
		jsonobject.put("total", resources.size());
		Struts2Utils.renderJson(jsonobject);
	}
	public Resource getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		return "list";
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}


}
